package test01.jdbc;

import test01.entity.User;

import java.sql.*;

public class JdbcDemo {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/userdb";
            Connection con = DriverManager.getConnection(url, "root", "123456");
            String sql="select * from t_users where id=?";
            PreparedStatement psta = con.prepareStatement(sql);
            psta.setInt(1,2);
            ResultSet res = psta.executeQuery();
            if (res.next()){
                int id = res.getInt(1);
                String name = res.getString(2);
                String password = res.getString(3);
                User user=new User(id,name,password);
                System.out.println(user.toString());
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
